package com.atguigu.gmall.realtime.function;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.util.*;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.hadoop.hbase.client.Connection;
import redis.clients.jedis.Jedis;



/**
 * @Author lzc
 * @Date 2023/5/4 14:25
 */
public abstract class DimMapFunctionHbase<T> extends RichMapFunction<T, T> implements DimFunction<T>{
    
    private Jedis redisClient;
    private Connection conn;
    
    @Override
    public void open(Configuration parameters) throws Exception {
        redisClient = RedisUtil.getRedisClient();
        conn = HbaseUtil.getHbaseConnection();
        
    }
    
    @Override
    public void close() throws Exception {
    
        HbaseUtil.closeHbaseConnection(conn);
        redisClient.close();  // 如果客户端是通过 new Jedis 得到, 则是是关闭客户端
        // 如果客户端是通过连接池得到的, 则是归还客户端
        
    }
 
    
    
    @Override
    public T map(T bean) throws Exception {
        JSONObject dim = DimUtilHbase.readDim(redisClient, conn,"gmall", getTable(), getId(bean));
        // 补充维度
        addDim(bean, dim);
        return bean;
    }
}
